package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.Entity.Category;
import com.sky.constant.StatusConstant;
import com.sky.dto.AddCategoryDTO;
import com.sky.dto.CategoryQueryDTO;
import com.sky.dto.UpdateCategoryDTO;
import com.sky.mapper.CategoryMapper;
import com.sky.service.CategoryService;
import com.sky.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void insert(AddCategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getEmpId());
//        category.setUpdateUser(BaseContext.getEmpId());
        category.setStatus(StatusConstant.DISABLE);
        Integer result=categoryMapper.insert(category);
    }

    @Override
    public PageVo<Category> pageQuery(CategoryQueryDTO categoryQueryDTO) {
        PageHelper.startPage(categoryQueryDTO.getPage(),categoryQueryDTO.getPageSize());
        Page<Category> page= categoryMapper.pageQuery(categoryQueryDTO);
        PageVo<Category> pageVo=new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRecords(page.getResult());
        return pageVo;
    }

    @Override
    public void update(UpdateCategoryDTO updateCategoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(updateCategoryDTO,category);
//        category.setUpdateUser(BaseContext.getEmpId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Category category=new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }

    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> pageQuery(Integer type) {
        return categoryMapper.queryByType(type);
    }

}
